package com.eims.sls_app.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.android.volley.toolbox.RequestParams;
import com.eims.sls_app.dataaccess.LuckdrawBean;
import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.ui.adapter.LuckWinAdapter;
import com.eims.sls_app.utils.App;

import bls.juejin.cn.R;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public class WinDrawFragment extends Fragment {
	private static final String TAG = "WinDrawFragment";
	private MyHandler myHandler;
	private MyAsynTask myAsynTask;
	private String opt, auth, info, time, imei, crc, key; // 格式化后的参数
	private ListView lv_win;
	private List<LuckdrawBean> luckdrawBean;
	private LuckWinAdapter luckWinAdapter;
	
	public static WinDrawFragment newInstance() {
		Bundle args = new Bundle();
		WinDrawFragment fragment = new WinDrawFragment();
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.notwin_fragment, null);
		myHandler = new MyHandler();
		
		initView(view);
		initData();
		
		return view;
	}
	
	
	private void initData() {
		myAsynTask = new MyAsynTask();
		myAsynTask.execute();
		
		
	}

	private void initView(View view) {
		lv_win = (ListView) view.findViewById(R.id.lv_not_draw);
		
		
	}


	/** 异步任务 */
	class MyAsynTask extends AsyncTask<Void, Integer, String> {


		/** 在后台执行的程序 */
		@Override
		protected String doInBackground(Void... params) {
			int p;
			info = "";

			//Log.i("x", "获得我全部投注记录    info  " + info);

			opt = "83";
			
				crc = RspBodyBaseBean.getCrc(time, imei, key, info,
						App.user.getUid());
			auth = RspBodyBaseBean.getAuth(crc, time, imei,
						App.user.getUid());
			
			
				Log.i(TAG,"抽奖>"+ RequestParams.convertParams(getActivity(), opt, info));
			String[] values = { opt, auth, info };
			String result = HttpUtils.doPost(App.names, values,
					App.path);

			Log.e("x", "得到所有抽奖内容   " + result);
			// MyToast.getToast(context, result);

			if ("-500".equals(result))
				return result;

			if (null == result)
				return "1";
			// 04-06 10:22:49.214: I/qtaguid(1826): Untagging socket 88 failed
			// errno=-13

			try {

				JSONArray item = new JSONArray(result);
				luckdrawBean = new ArrayList<LuckdrawBean>();
				for (int i = 0; i < item.length(); i++) {
					try {
						JSONObject jsonObject = item.getJSONObject(i);
						LuckdrawBean bean = new LuckdrawBean();
						
						bean.setId(jsonObject.optInt("id"));
						bean.setDatetime(jsonObject.optString("datetime"));
						bean.setSchemeid(jsonObject.getString("schemeid"));
						bean.setMoney(jsonObject.optDouble("money"));
						bean.setUid(jsonObject.optInt("uid"));
						luckdrawBean.add(bean);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					
				}
				
				

				if (this.isCancelled()) {
					Log.i("x", "取消了异步。。。。");
					return null;
				}
			} catch (Exception e) {
				e.printStackTrace();
				Log.i("x", "myAllLottery 错误" + e.getMessage());
			}

			return "0";
		}

		@Override
		protected void onPostExecute(String result) {
			if (null == result)
				return;
			if ("1".equals(result)) {
				myHandler.sendEmptyMessage(-1);
			}

			myHandler.sendEmptyMessage(Integer.parseInt(result));
			super.onPostExecute(result);
		}
	}
	
	/** 进行页面刷新 */
	@SuppressLint("HandlerLeak")
	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				if (luckdrawBean==null) {
					Toast.makeText(getActivity(), "数据为空", Toast.LENGTH_SHORT).show();
				}else {
					luckWinAdapter = new LuckWinAdapter(luckdrawBean, getActivity());
					lv_win.setAdapter(luckWinAdapter);
				}
				
				break;
			case -1:
				
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	}

}
